﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Security.Cryptography;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace _00365.Biz
{
    public static class Sys
    {
        public static Data.Login GetLoggedUser(HttpSessionState Session)
        {
            return (Session["LoggedUser"] as Data.Login);
        }

        public static List<Data.Feature> GetLoggedUserPermissions(HttpSessionState Session)
        {
            return (Session["UserPermissions"] as List<Data.Feature>);
        }

        public static void SetLoggedUser(HttpSessionState Session, Data.Login user)
        {
            Session["LoggedUser"] = user;
        }

        public static void SetLoggedUserPermissions(HttpSessionState Session, List<Data.Feature> permissions)
        {
            Session["UserPermissions"] = permissions;
        }

        public enum RowState
        {
            All,
            True,
            False
        }

        public static string CryptoKey
        {
            get
            {
                return "@##Al4R5ygdvny¨&42(7876;.<%==";
            }
        }

        public static string Encrypt(string plainText)
        {
            string passPhrase = CryptoKey;
            string saltValue = "(*&¨%$#@!QWSDFVBNMKIUYTREDFVMKI*&¨%$";
            string hashAlgorithm = "SHA1";
            int passwordIterations = 2;
            string initVector = "@1B2c3D4e5F6g7H8";
            int keySize = 128;

            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
            PasswordDeriveBytes password = new PasswordDeriveBytes(
                                                            passPhrase,
                                                            saltValueBytes,
                                                            hashAlgorithm,
                                                            passwordIterations);
            byte[] keyBytes = password.GetBytes(keySize / 8);
            RijndaelManaged symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform encryptor = symmetricKey.CreateEncryptor(
                                                             keyBytes,
                                                             initVectorBytes);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                         encryptor,
                                                         CryptoStreamMode.Write);
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
            cryptoStream.FlushFinalBlock();
            byte[] cipherTextBytes = memoryStream.ToArray();
            memoryStream.Close();
            cryptoStream.Close();
            string cipherText = Convert.ToBase64String(cipherTextBytes);
            return cipherText;
        }

        public static string Decrypt(string cipherText)
        {

            string passPhrase = CryptoKey;
            string saltValue = "(*&¨%$#@!QWSDFVBNMKIUYTREDFVMKI*&¨%$";
            string hashAlgorithm = "SHA1";
            int passwordIterations = 2;
            string initVector = "@1B2c3D4e5F6g7H8";
            int keySize = 128;

            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
            byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
            PasswordDeriveBytes password = new PasswordDeriveBytes(
                                                            passPhrase,
                                                            saltValueBytes,
                                                            hashAlgorithm,
                                                            passwordIterations);
            byte[] keyBytes = password.GetBytes(keySize / 8);
            RijndaelManaged symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
                                                             keyBytes,
                                                             initVectorBytes);
            MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                          decryptor,
                                                          CryptoStreamMode.Read);
            byte[] plainTextBytes = new byte[cipherTextBytes.Length];
            int decryptedByteCount = cryptoStream.Read(plainTextBytes,
                                                       0,
                                                       plainTextBytes.Length);
            memoryStream.Close();
            cryptoStream.Close();
            string plainText = Encoding.UTF8.GetString(plainTextBytes,
                                                       0,
                                                       decryptedByteCount);
            return plainText;
        }

        public static void SetBasicObjectProperties(object Entity, bool NewEntry, bool RowState, HttpSessionState Session)
        {
            if (NewEntry) PropValueSetter(Entity, "Created", DateTime.Now);
            PropValueSetter(Entity, "Modified", DateTime.Now);
            PropValueSetter(Entity, "RowState", RowState);
            PropValueSetter(Entity, "Username", (Sys.GetLoggedUser(Session) != null ? Sys.GetLoggedUser(Session).Username : "NotLoggedIn"));
        }

        public static bool PropValueSetter(Object obj, string propName, object newValue)
        {
            try
            {
                PropertyInfo prInfo = obj.GetType().GetProperty(propName);
                MethodInfo setter = prInfo.GetSetMethod();
                if (!setter.IsPublic)
                    return false;
                {
                    object value = prInfo.GetValue(obj, null);
                    prInfo.SetValue(obj, newValue, null);
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }



    }

}
